//
//  QBStatusRetweetView.swift
//  Wei91BoiOS
//
//  Created by bing on 16/3/27.
//  Copyright © 2016年 bing. All rights reserved.
//

import UIKit

import SnapKit

class QBStatusRetweetView: UIView {
    
    // MARK: - 属性
    var retweentViewBottomCons: Constraint?
    
    // 被转发的微博模型
    var retweetStatus: QBStatus? {
        didSet {
            // 当别人给被转发的模型赋值后,将模型的内容赋给对应的view
            // @发微博人的名字: 微博内容
            guard let screen_name = retweetStatus?.user?.screen_name else {
                QBPrint(msg: "retweetStatus?.user?.screen_name 没有值")
                return
            }
            
            /*
            guard let text = retweetStatus?.text else {
                QBPrint(msg: "retweetStatus?.text没有值")
                return
            }
            */
            guard let attrText = retweetStatus?.attrText else {
                QBPrint(msg: "retweetStatus?.text没有值")
                return

            }
            
            //retweentContentLabel.text = "@\(screen_name):\(text)"
            let nameAttr = NSMutableAttributedString(string: "@\(screen_name): ")
            nameAttr.appendAttributedString(attrText)
            
            retweentContentLabel.attributedText = nameAttr
            
            // 判断被转发的微博里面是否有配图
            self.retweentViewBottomCons?.uninstall()
            
            if let pictureURLs = retweetStatus?.storePictureURLs {
                // 有图片要显示,显示retweetPictureView
                retweetPictureView.hidden = false
                
                // 被转发微博图片URL赋值给retweetPictureView
                retweetPictureView.pictureURLs = pictureURLs
                
                // 重新设置retweetPictureView的底部约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.retweentViewBottomCons = make.bottom.equalTo(retweetPictureView).offset(QBStatusCellMargin).constraint
                })
            } else {
                // 没有图片显示
                retweetPictureView.hidden = true
                // 重新设置QBStatusRetweetView底部约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweentViewBottomCons = make.bottom.equalTo(retweentContentLabel).offset(QBStatusCellMargin).constraint
                })
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    private func prepareUI() {
        // 添加约束
        addSubview(bkgUIButton)
        addSubview(retweentContentLabel)
        addSubview(retweetPictureView)
        
        // 背景
        bkgUIButton.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        
        // 转发文本
        retweentContentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(QBStatusCellMargin)
            make.trailing.equalTo(self).offset(-QBStatusCellMargin)
        }
        
        // 被转发的微博配图
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(retweentContentLabel)
            make.top.equalTo(retweentContentLabel.snp_bottom).offset(QBStatusCellMargin)
            
            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        
        // QBStatusRetweetView底部和retweetContentLabel 重合,QBStatusRetweetView的高度根据子控件的内容来确定
        self.snp_makeConstraints { (make) -> Void in
            self.retweentViewBottomCons = make.bottom.equalTo(retweetPictureView).offset(QBStatusCellMargin).constraint
        }
    }

    // MARK: - 懒加载
    private lazy var bkgUIButton: UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
        return button
    }()
    
    /// 被转发微博内容label
    private lazy var retweentContentLabel: UILabel = {
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(QBStatusCellContentSize)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        
        // 换行/折行宽度
        label.preferredMaxLayoutWidth = UIScreen.width - 2 * QBStatusCellMargin
        
        
        return label
    }()
    
    /// 微博配图
    private lazy var retweetPictureView: QBStatusPictureView = QBStatusPictureView()
    
}
